﻿using CheemsControl.Animations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CheemsControl.Controls
{
    public class ImageDisplayWindow : Window
    {
        public ImageDisplayWindow()
        {
            this.Background = new SolidColorBrush(Color.FromArgb(0x80, 0x99, 0x99, 0x99));
            
        }

        /// <summary>
        /// 图片路径
        /// </summary>
        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(string), typeof(ImageDisplayWindow), new PropertyMetadata(null, OnSourceChanged));



        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageDisplayWindow), new PropertyMetadata(new CornerRadius(10)));



        /// <summary>
        /// 图片路径改变时触发
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageDisplayWindow thisControl = d as ImageDisplayWindow;

            // 外部边框
            Border border1 = new Border()
            {
                Background = new SolidColorBrush(Color.FromArgb(0x20, 0xEE, 0xEE, 0xEE)),
            };
            border1.MouseLeftButtonDown += (sender, args) =>
            {
                thisControl.Close();
            };

            // 内部边框
            RoundedCornerGrid roundedCornerGrid = new RoundedCornerGrid()
            {
                Margin = new Thickness(30, 20, 30, 20),
                Background = thisControl.Background,
                CornerRadius = thisControl.CornerRadius,
                ClipToBounds = true // 确保图片限制在 Border2 范围内
            };

            // 图片
            Image image = new Image()
            {
                Margin = new Thickness(20),
                Source = new BitmapImage(new Uri(thisControl.Source)),
                RenderTransform = new TranslateTransform(), // 用于拖动
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            image.Loaded += (sender, args) =>
            {
                AnimationHelper.EnableDragAndScale(image);
                // AnimationHelper.EnableScale(image);

            };

            // 将图片添加到内部边框
            roundedCornerGrid.Children.Add(image);

            // 将内部边框添加到外部边框
            border1.Child = roundedCornerGrid;

            // 设置控件内容
            thisControl.Content = border1;
        }

    }
}
